package com.ohimy.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.ohimy.entity.Group;
import com.ohimy.entity.GroupCate;
import com.ohimy.entity.GroupPhoto;
import com.ohimy.entity.GroupUserShip;
import com.ohimy.entity.User;
import com.ohimy.service.impl.ForumService;
import com.ohimy.service.impl.GroupService;
import com.ohimy.service.impl.PhotoService;
import com.ohimy.service.impl.UserService;
import com.ohimy.util.FileManager;

@Controller("groupAction")
public class GroupAction extends BaseAction {

	private static final long serialVersionUID = -4346667373583689606L;

	@Resource(name = "groupService")
	private GroupService groupService;

	@Resource(name = "forumService")
	private ForumService forumService;

	@Resource(name = "photoService")
	private PhotoService photoService;

	@Resource(name = "userService")
	private UserService userService;

	private Group group = new Group();
	private GroupCate groupCate = new GroupCate();
	private GroupUserShip groupUserShip = new GroupUserShip();
	private GroupPhoto groupPhoto = new GroupPhoto();
	private User user = new User();
	private List<User> member;
	private List<User> newmember;
	private List<Group> groupList;
	private List<GroupCate> groupCateList;
	private List<GroupPhoto> groupPhotoList;
	private long total;
	private String gid, page_index, maxResult;

	private File fileData;
	private String fileDataFileName;
	private String fileDataContentType;

	@Override
	public String execute() throws Exception {
		// 当用户没有登录时 跳转至群组列表
		// 如果已经登陆 进入群组主页
		if (super.getSession() == null) {
			groupList = groupService.findAllGroup();
			groupCateList = groupService.findAllGroupCate();
			result = "/WEB-INF/site/group/list.jsp";
		} else {
			result = "/WEB-INF/site/group/";
		}
		return "ohimy";
	}

	/**
	 * 根据群组ID进入该主页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String view() throws Exception {
		// 得到群组资料
		group = groupService.getGroupById(id);
		// 得到创建用户资料
		user = userService.findUserById(group.getFounder());
		// 得到群组会员信息
		member = groupService.getMember(id);
		// 得到最近上传的10张照片
		groupPhotoList = photoService.getPhotoAllInTen(id);
		// 
		total = forumService.getForumByTotal(id);
		if (super.getSession() != null)
			result = "/WEB-INF/site/group/_index.jsp";
		else
			result = "/WEB-INF/site/group/_index_nos.jsp";
		return "ohimy";
	}

	/**
	 * 转向成员显示页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String members() throws Exception {
		group = groupService.getGroupById(id);
		total = groupService.getGroupMemberNumber(id);
		newmember = groupService.getNewMember(id);
		result = "/WEB-INF/site/group/member_list.jsp";
		return "ohimy";
	}

	/**
	 * 保存一个群组信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		// 保存一个群组
		group.setFounder(super.getSession().getId());
		group.setCreated(new Date());
		group.setGroupHeader("default.jpg");
		groupCate = groupService.getGroupCateById((long) group.getGroupCate()
				.getId());
		groupCate.getGroup().add(group);
		group.setGroupCate(groupCate);
		groupService.createGroup(group);

		// 将创建者加入刚创建的群组
		groupUserShip.setGroup(group);
		groupUserShip.setUser(userService.findUserById(getSession().getId()));
		groupUserShip.setCreated(new Date());
		groupService.saveShip(groupUserShip);

		return null;
	}

	/**
	 * 转向新建群组页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String create() throws Exception {
		result = "/WEB-INF/site/group/new.jsp";
		return "ohimy";
	}

	/**
	 * 转向相册列表页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String photos() throws Exception {
		group = groupService.getGroupById(id);
		total = photoService.getPhotoByTotal(id);
		result = "/WEB-INF/site/photos/g_view.jsp";
		return "ohimy";
	}

	/**
	 * ajax远程调用的相册列表 每页显示18张图片
	 * 
	 * @return
	 * @throws Exception
	 */
	public String page() throws Exception {
		long gid = Long.parseLong(this.gid);
		int page_index = Integer.parseInt(this.page_index);
		int maxResult = Integer.parseInt(this.maxResult);
		groupPhotoList = photoService.getPhotoByPager(gid, page_index
				* maxResult, maxResult);
		result = "/WEB-INF/site/ajax/group_photo_list.jsp";
		return "ohimy";
	}

	/**
	 * ajax远程调用的成员列表 每页显示16个成员
	 * 
	 * @return
	 * @throws Exception
	 */
	public String page4Member() throws Exception {
		long gid = Long.parseLong(this.gid);
		int page_index = Integer.parseInt(this.page_index);
		int maxResult = Integer.parseInt(this.maxResult);
		member = groupService.getMemberByPager(gid, page_index * maxResult,
				maxResult);
		result = "/WEB-INF/site/ajax/group_member_list.jsp";
		return "ohimy";
	}

	/**
	 * 跳转到添加相册页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addPhotos() throws Exception {
		group = groupService.getGroupById(id);
		result = "/WEB-INF/site/photos/g_add.jsp";
		return "ohimy";
	}

	/**
	 * 保存上传的相片
	 * 
	 * @return
	 * @throws Exception
	 */
	public String uploadPhotos() throws Exception {
		String pageErrorInfo = null;
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			FileManager fileManager = new FileManager();
			String url = fileManager.generateFileName(this.fileDataFileName);
			String filePath = ServletActionContext.getServletContext()
					.getRealPath("upload")
					+ "/" + url;
			File targetFile = new File(filePath);
			pageErrorInfo = fileManager.upLoadFile(this.fileData, targetFile);
			if (pageErrorInfo == null) {
				response.getWriter().write("successed");
			}
			// 将信息写进数据库
			groupPhoto.setCreated(new Date());
			groupPhoto.setGroup(groupService.getGroupById(id));
			groupPhoto.setPublisher(userService.findUserById(getSession()
					.getId()));
			groupPhoto.setUrl(url);
			photoService.save(groupPhoto);
		} catch (Exception e) {
			pageErrorInfo = e.getMessage();
		} finally {
			if (pageErrorInfo != null) {
				response.getWriter().write(pageErrorInfo);
			}
		}
		return null;
	}

	/**
	 * 加入一个群组
	 * 
	 * @return
	 * @throws Exception
	 */
	public String join() throws Exception {
		groupUserShip.setGroup(groupService.getGroupById(id));
		groupUserShip.setUser(userService.findUserById(getSession().getId()));
		groupUserShip.setCreated(new Date());
		groupService.saveShip(groupUserShip);

		result = "/WEB-INF/site/group/new.jsp";
		return "ohimy";
	}

	/* ******************************************* */
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public List<Group> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}

	public List<GroupCate> getGroupCateList() {
		return groupCateList;
	}

	public void setGroupCateList(List<GroupCate> groupCateList) {
		this.groupCateList = groupCateList;
	}

	public GroupCate getGroupCate() {
		return groupCate;
	}

	public void setGroupCate(GroupCate groupCate) {
		this.groupCate = groupCate;
	}

	public GroupUserShip getGroupUserShip() {
		return groupUserShip;
	}

	public void setGroupUserShip(GroupUserShip groupUserShip) {
		this.groupUserShip = groupUserShip;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public GroupPhoto getGroupPhoto() {
		return groupPhoto;
	}

	public void setGroupPhoto(GroupPhoto groupPhoto) {
		this.groupPhoto = groupPhoto;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setPhotoService(PhotoService photoService) {
		this.photoService = photoService;
	}

	public List<User> getMember() {
		return member;
	}

	public void setMember(List<User> member) {
		this.member = member;
	}

	public List<GroupPhoto> getGroupPhotoList() {
		return groupPhotoList;
	}

	public void setGroupPhotoList(List<GroupPhoto> groupPhotoList) {
		this.groupPhotoList = groupPhotoList;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}

	public String getPage_index() {
		return page_index;
	}

	public void setPage_index(String page_index) {
		this.page_index = page_index;
	}

	public File getFileData() {
		return fileData;
	}

	public void setFileData(File fileData) {
		this.fileData = fileData;
	}

	public String getFileDataFileName() {
		return fileDataFileName;
	}

	public void setFileDataFileName(String fileDataFileName) {
		this.fileDataFileName = fileDataFileName;
	}

	public String getFileDataContentType() {
		return fileDataContentType;
	}

	public void setFileDataContentType(String fileDataContentType) {
		this.fileDataContentType = fileDataContentType;
	}

	public List<User> getNewmember() {
		return newmember;
	}

	public void setNewmember(List<User> newmember) {
		this.newmember = newmember;
	}

	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}

	public String getMaxResult() {
		return maxResult;
	}

	public void setMaxResult(String maxResult) {
		this.maxResult = maxResult;
	}

}